/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManager.Book.Logic;

import Data.DAO.IBookStore;
import Data.DAO.IDataStore;
import data.pojo.Book;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author kite
 */
public class EditBookManage {

    
    private List<Book> editList = new ArrayList<Book>();
    private List<Book> searchList = new ArrayList<Book>();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private IDataStore bookDS = new IBookStore();
    private List<Book> allList;

    public EditBookManage() {
        this.allList = (ArrayList<Book>) bookDS.getObjects();
    }
     public Vector<List> searchTableVector(String key, String keywords) {
         searchList = bookDS.getObjectsByAttr(key, ".*" + keywords + ".*");
        return new Vector<List>(toBookTable(searchList));
    }

    public Vector<List> bookTableVector() {
        return new Vector<List>(toBookTable(editList));
    }

    public Vector<Vector<String>> toBookTable(List list) {
        try {
            Vector<Book> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toBookStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toBookStringArray(Book book) {
        Vector<String> list = new Vector<String>();
        list.add(book.getISBN());//ISBN
        list.add(book.getTitle());//title
        list.add(book.getAuthor());//Author
        list.add(((Double) book.getPrice()).toString());//price     
        list.add(String.valueOf(book.getPublisher()));//publicsher
        list.add("");//publicationDate
        return list;
    }
    
   public boolean addToEditList(int index,Book book) {
        if (this.isValid(book)) {
            this.indexes.add(index);
            this.editList.add(book);
            return true;
        } else {
            return false;
        }
    }

    private boolean isValid(Book book) {
        if (!this.bookDS.searchObjectsByAttr("ISBN", String.valueOf(book.getISBN())).isEmpty()) {
            return false;
        }
        for (Book b : allList) {
            if (b.getISBN() == book.getISBN()) {
                return false;
            }
        }
        return true;
    }
    
   public Book getBySearch(int index) {
        return this.searchList.get(index);
    }
 

    public int getIndexInAll(Book book) {
        for(int i=0; i<allList.size(); i++){
            if(allList.get(i).getISBN().equals(book.getISBN())){
                return i;
            }
        }
        return -1;
    }


   
    public boolean deleteFromUpdatedList(int index) {
        if(index<=indexes.size() && index>=0){
            this.indexes.remove(index);
            this.editList.remove(index);
            return true;
        }
        return false;
    }


    public boolean commit() {
        for(int i=0; i<indexes.size(); i++){
            allList.set(this.indexes.get(i), this.editList.get(i));
        }
        bookDS.update(allList);
        this.editList.clear();
        this.indexes.clear();
        return true;
    }

 
    public List<Book> getEditList() {
        return this.editList;
    }

   
    
}
